package com.woniu.config;

import io.modelcontextprotocol.client.McpClient;
import io.modelcontextprotocol.spec.McpSchema;
import jakarta.annotation.Resource;
import org.springframework.ai.chat.model.StreamingChatModel;
import org.springframework.ai.mcp.customizer.McpAsyncClientCustomizer;
import org.springframework.ai.zhipuai.ZhiPuAiChatModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Mono;

import java.time.Duration;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: 朱杰
 * @Description: 系统边界配置
 * @DateTime: 2025/6/10 10:34
 **/
@Component
public class CustomMcpAsyncClientCustomizer implements McpAsyncClientCustomizer {
    @Autowired
    ZhiPuAiChatModel zhiPuAiChatModel;
    @Resource(name = "zhiPuAiChatModel")
    StreamingChatModel streamingChatModel;

    @Override
    public void customize(String serverConfigurationName, McpClient.AsyncSpec spec) {
        // addRoot根定义了服务器在文件系统中可以运行的位置的边界
        List<McpSchema.Root> roots = new ArrayList<>();
        roots.add(new McpSchema.Root("file://D:/Desktop", "description"));
        spec.roots(roots);

        // 配置日志等级
        // 修正日志处理逻辑
        spec.loggingConsumer(notification -> {
            // 创建新通知对象而不是修改参数
            McpSchema.LoggingMessageNotification newNotification = McpSchema.LoggingMessageNotification.builder()
                    .level(McpSchema.LoggingLevel.DEBUG)
                    .build();
            System.out.println(newNotification.level().level());
            return Mono.empty();
        });

        spec.sampling(createMessageRequest -> {
            // 修改请求
            modifyRequest(createMessageRequest);
            // 调用模型处理请求
            return Mono.empty();
        });
        spec.requestTimeout(Duration.ofSeconds(30));
    }

    private void modifyRequest(McpSchema.CreateMessageRequest request) {
        // 修复语法错误：补充正确的方法名 无效果，还需要研究下
        String originalMessage = request.systemPrompt();
        System.out.println("系统提示词：" + originalMessage);
    }

//    private Mono<McpSchema.CreateMessageResult> processRequest(McpSchema.CreateMessageRequest request) {
//        // 示例：调用模型处理请求
//        // 这里可以根据实际情况调用具体的模型处理方法
//        // 假设这里有一个方法可以处理请求并返回结果
//        return Mono.just(new McpSchema.CreateMessageResult(request.systemPrompt(),
//                "模型处理结果", null, null));
//    }
}
